<template>
  <div id="commentMange">
    <!-- 上部搜索条件 -->
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <!-- 操作按钮部分 -->
    <div class="table-conBtn">
      <el-button
        type="warning"
        v-permission="'wh:system:autoCommentMange:Import'"
        icon="el-icon-upload2"
        @click="importBtn"
      >
        导入
      </el-button>
      <el-button
        type="warning"
        v-permission="'wh:system:autoCommentMange:batchRemove'"
        plain
        @click="deleteSome"
      >
        批量删除
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
    </div>
    <!-- 表格部分 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      height="490"
      @selection-change="selectChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" width="60" label="序号">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column prop="level" label="等级" min-width="100">
        <template slot-scope="scope">
          <span
            v-if="scope.row.level"
            class="tag-span"
            :class="scope.row.level | filterClass('autoLevelType')"
            >{{ scope.row.level | filterName('autoLevelType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="collegeId" label="所属学院" min-width="100">
        <template slot-scope="scope">
          <span
            v-if="scope.row.collegeId !== null"
            class="tag-span"
            :class="scope.row.collegeId | filterClass('autoCollegeType')"
            >{{ scope.row.collegeId | filterName('autoCollegeType') }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="内容类型" min-width="100">
        <template slot-scope="scope">
          <span
            v-if="scope.row.type"
            class="tag-span"
            :class="scope.row.type | filterClass('autoContentType')"
            >{{ scope.row.type | filterName('autoContentType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="topicType" label="所属频道" min-width="100">
        <template slot-scope="scope">
          <span
            v-if="scope.row.topicType"
            class="tag-span"
            :class="scope.row.topicType | filterClass('topicType')"
            >{{ scope.row.topicType | filterName('topicType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        label="内容"
        min-width="140"
      ></el-table-column>
      <el-table-column
        prop="operatorUser.userName"
        label="创建者"
        min-width="120"
      ></el-table-column>
      <el-table-column
        prop="createdDate"
        label="创建时间"
        min-width="150"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="90">
        <template slot-scope="scope">
          <el-button
            v-permission="'wh:system:autoCommentMange:remove'"
            type="text"
            @click="deleteOne(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <!-- 导入弹窗 -->
    <importDialog
      v-if="importDialogData.isShow"
      :dialog-data="importDialogData"
      @refresh="refresh"
      @showError="showError"
    ></importDialog>
    <!-- 导入失败弹窗 -->
    <errorDialog
      v-if="errorDialogData.isShow"
      :dialog-data="errorDialogData"
      from="autoCommitWord"
    ></errorDialog>
  </div>
</template>
<script>
import {
  getSearchList,
  batchRemove,
  importUrl,
  downUrl
} from '@/api/autoComment/commentMange' // 接口
import topSearch from '@component/autoComment/commentMange/topSearch' // 上部搜索条件
import importDialog from '@common/importDialog' // 导入弹窗
import errorDialog from '@common/errorDialog' // 导入失败弹窗
import { saveScrollMixins } from '@/mixins/saveScroll'
export default {
  name: 'voluntarily',
  msg: '帖子自动维护/自动评论库管理',
  mixins: [saveScrollMixins],
  components: {
    topSearch,
    importDialog,
    errorDialog
  },
  data() {
    return {
      loading: false,
      total: 0,
      // 搜索条件
      formInline: {
        level: '', // 等级,默认所有
        type: '', // 类型,默认所有
        collegeId: '', // 所属学院,默认所有
        topicType: '', //所属频道
        searchContent: '' // 搜索框内容
      },
      // 表格数据
      tableData: [],
      // 已经选中的行数据
      selectData: [],
      canShowPage: false, // 是否显示当前页面
      powerStr: 'system:autoCommentMange:list', // 是否查询的标识
      powerAry: [], // 可显示的权限
      // 分页数据
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10, // 页面显示条数
        orderByColumn: 'createTime', // 排序字段
        isAsc: 'desc' // 正序倒序
      },
      // 传递给导入弹框的数据
      importDialogData: {
        title: '导入评论语',
        isShow: false, // 控制弹框显示
        params: '' // 参数
      },
      // 导入失败弹框的数据
      errorDialogData: {
        isShow: false, // 控制弹框显示
        warningText: '' // 参数
      }
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    this.searchList()
    // 获取权限
    // this.getPower()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
      }, 1000)
    })
  },
  methods: {
    /* 获取权限 */
    getPower() {
      let id = this.$route.query.mId
      this.getCurPageMenu(id).then((res) => {
        this.powerAry = res
        this.canShowPage = true
        if (!res.includes(this.powerStr)) {
          this.canGetList = false
          this.alertPower(this.powerStr)
        }
        this.searchList()
      })
    },
    // 获取列表
    searchList() {
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      getSearchList(
        obj,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // 导入按钮点击事件
    importBtn() {
      this.importDialogData.url = importUrl() // 导入接口
      this.importDialogData.downUrl = downUrl() // 下载模板接口
      this.importDialogData.isShow = true
    },
    /* 导入失败弹窗 */
    showError(text) {
      this.errorDialogData.isShow = true // 显示导入失败弹框
      this.errorDialogData.warningText = text // 显示导入失败弹框
    },
    // 选择框改变事件
    selectChange(value) {
      this.selectData = value
    },
    // 多选删除按钮
    deleteSome() {
      if (this.selectData.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      this.$confirm(
        `确认要删除选中的${this.selectData.length}条数据吗？`,
        '删除提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let ids = []
        this.selectData.forEach((item) => {
          ids.push(item.id)
        })
        this.delMulRequest(ids)
      })
    },
    // 单选删除按钮
    deleteOne(id) {
      this.$confirm(`确定要删除该条评论库信息吗？`, '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delMulRequest([id])
      })
    },

    /* 删除多个请求 */
    delMulRequest(ids) {
      let obj = { ids }
      batchRemove(
        obj,
        (response) => {
          this.searchList()
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    /* 新增后，刷新列表 */
    refresh() {
      this.toSearch()
    }
  }
}
</script>
<style lang="less" scoped>
#commentMange {
  .el-table {
    margin-top: 20px;
  }
}
</style>
